//
//  YQHomePageController.m
//  EPA--1
//
//  Created by lanou3g on 16/4/15.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQHomePageController.h"
#import "CK_MenuViewController.h"
#import "YQNetWorkRequestManager.h"
#import "YQCollectionViewCell.h"
#import "YQHomeModel.h"
#import "YQCycleView.h"
#import "YQHomeDetailController.h"

#import "YQTalkTableViewController.h"
#import "YQRadioListViewController.h"
@interface YQHomePageController ()<MenuClickDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
/**
 *  collectionView
 */
@property (nonatomic, strong) UICollectionView *collectionView;
/**
 *  数组属性
 */
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
/**
 *  轮播图属性
 */
@property (nonatomic, strong) YQCycleView *cycleView;
@end

@implementation YQHomePageController
- (void)didClickMenuIndex:(NSInteger)index Title:(NSString *)title
{

    [self.navigationController popToRootViewControllerAnimated:YES];
    switch (index) {
        case 1:
        {
            YQRadioListViewController *radioVC = [YQRadioListViewController new];
            [self.navigationController pushViewController:radioVC animated:YES];
        }
            break;
            case 3:
        {
            YQTalkTableViewController *talkVC = [YQTalkTableViewController new];
            [self.navigationController pushViewController:talkVC animated:YES];
            
        }
        default:
            break;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [CK_MenuViewController getMenuViewController].menuDelegate = self;
    [CK_MenuViewController getMenuViewController].menuArray = @[@"今日~TODAY",@"电台~TOPICS",@"阅读~READING",@"话题~TOPOCS",@"音乐~MUSIC",@"良品~PRODUCTS",@"订阅~SUBSCRITION",@"设置~SECTING"].mutableCopy;
    self.dataSourceArray = [NSMutableArray new];
    [YQNetWorkRequestManager requestURL:[NSURL URLWithString:READLIST_URL] method:YQ_GET params:nil result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *dataDict = dict[@"data"];
            for (NSDictionary *itemDict in dataDict[@"list"]) {
                YQHomeModel *model = [YQHomeModel new];
                [model setValuesForKeysWithDictionary:itemDict];
                [self.dataSourceArray addObject:model];
            }
    }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.collectionView reloadData];
            self.cycleView.imgNameArray = @[@"h1.jpeg",@"h2.jpeg",@"h3.jpeg",@"h4.jpeg",@"h5.jpeg",@"h6.jpeg",@"h7.jpeg",].mutableCopy;
        });

    }];
    //
     self.cycleView = [[YQCycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:3];
    
    //初始化collectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 10;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor brownColor];
//    //设置layout
    layout.itemSize = CGSizeMake(123, 186 );
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 200);
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self.view addSubview:self.collectionView];
    //注册cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"YQCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    //注册头试图
    [self.collectionView registerClass:[UICollectionReusableView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
#pragma mark - UICollectionView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YQHomeDetailController *detailVC = [YQHomeDetailController new];
    YQHomeModel *model = self.dataSourceArray[indexPath.row];
    detailVC.type = [NSString stringWithFormat:@"%@",model.type];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YQCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataSourceArray[indexPath.row];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//         NSLog(@"33333");
        
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        [header addSubview: self.cycleView];
//        header.userInteractionEnabled = YES;
        return header;
    }
    return nil;
}

/**
 *  点击抽屉按钮
 *
 *  @param sender 按钮对象
 */
- (IBAction)didClickBarButtonAction:(UIBarButtonItem *)sender {
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else {
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
